/*******************************************************************************
 * Copyright 2010 MoBrite (A venture of Kinara Systems Private Limited)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.mobrite.domaindroid;

import com.mobrite.domaindroid.db.DomainDroidDbAdapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class Preferences {

	public static final String SORT_OPTION	= "orderBy";
	
	public static final String 	PREF_TLD_CHECKBOX_1	= "com";
	public static final String 	PREF_TLD_CHECKBOX_2	= "net";
	public static final String 	PREF_TLD_CHECKBOX_3	= "org";
	public static final String 	PREF_TLD_CHECKBOX_4	= "in";
	public static final String 	PREF_TLD_CHECKBOX_5	= "de";
	public static final String 	PREF_TLD_CHECKBOX_6	= "eu";
	public static final String 	PREF_TLD_CHECKBOX_7	= "us";
	public static final String 	PREF_TLD_CHECKBOX_8	= "uk";
	public static final String 	PREF_TLD_CHECKBOX_9	= "biz";
	public static final String 	PREF_TLD_CHECKBOX_10	= "info";
	public static final String 	PREF_TLD_CHECKBOX_11	= "mobi";
	public static final String 	PREF_TLD_CHECKBOX_12	= "ch";

	private Context context = null;

	public Preferences(Context context) {
		super();
		this.context = context;
	}

	private String getString(String key, String def) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		String s = prefs.getString(key, def);
		return s;
	}

	private int getInt(String key, int def) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		int i = Integer.parseInt(prefs.getString(key, Integer.toString(def)));
		return i;
	}

	private float getFloat(String key, float def) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		float f = Float.parseFloat(prefs.getString(key, Float.toString(def)));
		return f;
	}

	private long getLong(String key, long def) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		long l = Long.parseLong(prefs.getString(key, Long.toString(def)));
		return l;
	}

	private void setString(String key, String val) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor e = prefs.edit();
		e.putString(key, val);
		e.commit();
	}

	private void setBoolean(String key, boolean val) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor e = prefs.edit();
		e.putBoolean(key, val);
		e.commit();
	}

	private void setInt(String key, int val) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor e = prefs.edit();
		e.putString(key, Integer.toString(val));
		e.commit();
	}

	private void setLong(String key, long val) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor e = prefs.edit();
		e.putString(key, Long.toString(val));
		e.commit();
	}

	private boolean getBoolean(String key, boolean def) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		boolean b = prefs.getBoolean(key, def);
		return b;
	}

	private int[] getIntArray(String key, String def) {
		String s = getString(key, def);
		int[] ia = new int[s.length()];
		for (int i = 0; i < s.length(); i++) {
			ia[i] = s.charAt(i) - '0';
		}
		return ia;
	}

	/*
	 * Public methods to get/set Preferences
	 */
	
	public String getSortingOption() {
		return getString(SORT_OPTION, DomainDroidDbAdapter.KEY_DOMAIN);
	}
	
	public void setSortingOption(String sortOption) {
		setString(SORT_OPTION, sortOption);
	}
}
